package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author likepei
 * @Date 2020/3/26 2:51
 * @Version v1.0
 * @Description 用户业务接口实现类
 */
@Service
public class UserServiceImpl implements UserService {

    //依赖注入
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;

    /**
     * 基于用户名 获取User对象
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        //通过 用户名获取用户信息
        User user = userDao.findByUsername(username);
        //通过 用户名id  获取所关联的角色集合
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        user.setRoles(roleSet);
        //通过 每一个角色id  获取所关联的权限集合
        for (Role role : roleSet) {
            Set<Permission> permissionSet = permissionDao.findByRoleId(role.getId());
            role.setPermissions(permissionSet);
        }

        return user;
    }

    @Override
    public boolean login(String username, String password) {
        System.out.println("service_provide=======u:" + username + " p:" + password);
        if ("admin".equals(username) && "123".equals(password)){
            return true;
        }
        return false;
    }

    /**
     * 根据用户名获取对应的菜单列表
     * @param username
     * @return
     */
    @Override
    public List<Map<String, Object>> getMenuListByUsername(String username) {
        // 存放父菜单数据的集合
        List<Map<String, Object>> parentMapMenuList = new ArrayList<>();
        // 存放子菜单数据的集合
        List<Map<String, Object>> childrenMapMenuList = new ArrayList<>();

        //通过 用户名获取用户信息
        User user = userDao.findByUsername(username);
        //通过 用户名id  获取所关联的角色集合
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        //通过 每一个角色id  获取所关联的菜单集合
        for (Role role : roleSet) {
            List<Menu> menuList = menuDao.findByRoleId(role.getId());
            // 遍历所有菜单的集合
            for (Menu menu : menuList) {

                // 封装父菜单的集合
                Map<String, Object> parentMenuMap = new HashMap<>();
                // 封装子菜单的集合
                Map<String, Object> childrenMenuMap = new HashMap<>();

                // 父菜单
                if (menu.getParentMenuId() == null) {
                    parentMenuMap.put("priority", menu.getPriority());
                    parentMenuMap.put("id", menu.getId());
                    parentMenuMap.put("path", menu.getPath());
                    parentMenuMap.put("title", menu.getName());
                    parentMenuMap.put("icon", menu.getIcon());
                    parentMapMenuList.add(parentMenuMap);
                } else {
                    // 子菜单
                    childrenMenuMap.put("priority", menu.getPriority());
                    childrenMenuMap.put("parentMenuId", menu.getParentMenuId());
                    childrenMenuMap.put("path", menu.getPath());
                    childrenMenuMap.put("title", menu.getName());
                    childrenMenuMap.put("linkUrl", menu.getLinkUrl());
                    childrenMapMenuList.add(childrenMenuMap);
                }
            }
        }

        // 根据优先权priority的值对菜单的展示顺序排序
        /*
            Collections.sort(parentMapMenuList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return (int) o1.get("priority") - (int) o2.get("priority");
                }
            });
        */
        parentMapMenuList.sort(Comparator.comparingInt(o -> (int) o.get("priority")));
        childrenMapMenuList.sort(Comparator.comparingInt(o -> (int) o.get("priority")));

        // 将子菜单封装到父菜单中
        for (Map<String, Object> parentMenuMap : parentMapMenuList) {
            List<Map<String, Object>> childrenMapList = new ArrayList<>();
            Integer id = (Integer) parentMenuMap.get("id");
            for (Map<String, Object> childrenMenuMap : childrenMapMenuList) {
                Integer parentMenuId = (Integer) childrenMenuMap.get("parentMenuId");
                if (parentMenuId.equals(id)) {
                    childrenMapList.add(childrenMenuMap);
                }
            }
            parentMenuMap.put("children", childrenMapList);
        }

        return parentMapMenuList;
    }


    /*
       分页查询所有用户
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        try {
            //配置分页助手PageHelper
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //根据条件调用dao查询数据
            Page<User> page = userDao.findPage(queryPageBean.getQueryString());
            //返回 总计入数,当前页查询结果
            return new PageResult(page.getTotal(),page.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    //保存用户
    @Override
    public void add(User user) {
        userDao.add(user);
    }
    //根据id 查询用户信息
    @Override
    public User findById(Integer id) {
        User user = userDao.findById(id);
        return user;
    }

    // 编辑用户信息
    @Override
    public void edit(User user) {
        userDao.edit(user);
    }

    //删除用户信息
    @Override
    public void delete(Integer id) {
        userDao.delete(id);
    }
}
